<script setup>
import { ref, reactive, getCurrentInstance, onMounted, watch, render, nextTick } from "vue"
import {mw_throttle} from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus';

const instance = getCurrentInstance()
const _this = instance.appContext.config.globalProperties //vue3获取当前this

import { globalStore } from "@/store/global";
const globalVue = globalStore();

// 组件中使用，接收传参的
let propsData = defineProps({
  searchForm: Object,
  selectHospital: {
    type: Array,
    default: []
  },
  selectHandle: {
    type: Array,
    default: []
  },
  selectAlarmName: {
    type: Array,
    default: []
  },
  selectLevel: {
    type: Array,
    default: []
  },
  selectCity: {
    type: Array,
    default: []
  },
  selectClassification: {
    type: Array,
    default: []
  },
  
})

// 组件中使用，接收事件的
const emits = defineEmits(['onSearch']);

onMounted(async () => {
  organizationSelect.value = await globalVue.getSelect('organizationSelect', { type: propsData.searchForm.type || '' });
  citySelect.value = await globalVue.getSelect('newCitySelect', { hospitalId: propsData.searchForm.hospitalIds || '' });
  selectVehicle.value = await globalVue.getSelect('alarmVehicleSelect', {});
})

const onSubmit = () => {
  function _submit(){
    emits('onSearch')
  }
  mw_throttle(_submit, 1000)
}

const organizationSelect = ref([])
const citySelect = ref([])
const selectVehicle = ref([])

async function clearSubmit(num){
  if (num == 1) {
    propsData.searchForm.hospitalIds = null;
    propsData.searchForm.city = null;
    propsData.searchForm.ambulanceId = null;
    organizationSelect.value = await globalVue.getSelect('organizationSelect', { type: propsData.searchForm.type || '' });
    citySelect.value = await globalVue.getSelect('newCitySelect', {});
    selectVehicle.value = await globalVue.getSelect('alarmVehicleSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalIds || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
  }else if(num ==2) {
    propsData.searchForm.city = null;
    propsData.searchForm.ambulanceId = null;
    citySelect.value = await globalVue.getSelect('newCitySelect', { hospitalId: propsData.searchForm.hospitalIds || '' });
    selectVehicle.value = await globalVue.getSelect('alarmVehicleSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalIds || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
  }else if(num == 3){
    propsData.searchForm.ambulanceId = null;
    selectVehicle.value = await globalVue.getSelect('alarmVehicleSelect', {type: propsData.searchForm.type || '', hospitalId: propsData.searchForm.hospitalIds || '', city: propsData.searchForm.city ? propsData.searchForm.city+'' : ''});
  }
  onSubmit()
}

</script>

<template>
  <el-form :inline="true" :model="searchForm" class="form_search_table">
    <el-form-item label="Classification">
      <el-select class="value_input search_input_130" v-model="searchForm.type" @change="clearSubmit(1)" placeholder="Please select" filterable clearable>
        <el-option v-for="item in selectClassification" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Organization">
      <el-select class="value_input" v-model="searchForm.hospitalIds" @change="clearSubmit(2)" placeholder="Please select" filterable clearable>
        <el-option v-for="item in organizationSelect" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="City">
      <el-select v-model="searchForm.city" @change="clearSubmit(3)" placeholder="Please select"  multiple collapse-tags filterable clearable>
        <el-option v-for="item in citySelect" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Code Number">
      <el-select @change="onSubmit" class="search_input_130" v-model="searchForm.ambulanceId" placeholder="Please select" filterable clearable>
        <el-option v-for="item in selectVehicle" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Level">
      <el-select @change="onSubmit" class="search_input_130" v-model="searchForm.level" placeholder="Please select" filterable clearable>
        <el-option v-for="item in selectLevel" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Algorithm">
      <el-select @change="onSubmit" class="search_input_130" v-model="searchForm.alarmType" placeholder="Please select" filterable clearable>
        <el-option v-for="item in selectAlarmName" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Handle">
      <el-select @change="onSubmit" class="search_input_130" v-model="searchForm.status" placeholder="Please select" filterable clearable>
        <el-option v-for="item in selectHandle" :key="item.id" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="Time">
      <el-date-picker @change="onSubmit" v-model="searchForm.date" value-format="YYYY-MM-DD HH:mm:ss" type="datetimerange" range-separator="To" start-placeholder="Start date" end-placeholder="End date" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">Query</el-button>
    </el-form-item>
  </el-form>
</template>

<style lang="less">
.form_search_table {
  .search_input_level {
    .el-input {
      width: 60px;
    }
  }

  .search_input_130 {
    .el-input {
      width: 130px;
    }
  }
  
  .search_input_180 {
    .el-input {
      width: 180px;
    }
  }

}
</style>